<template>
  <div class="login">
    <el-form status-icon ref="form" :rules="rules" :model="form" label-width="80px">
      <img class="tou" src="../assets/ti.jpg" alt />
      <el-form-item label="用户名" prop="username">
        <el-input @keyup.enter.native='login' placeholder="请输入用户名" v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input @keyup.enter.native='login' placeholder="请输入密码" type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="login" type="primary" class="loginBtn">登录</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 导入AXIOS
import axios from 'axios'

export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: ['blur', 'change']
          },
          {
            min: 3,
            max: 6,
            message: '账号长度在 3 到 6 个字符',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['blur', 'change']
          },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 个字符',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    login () {
      // 先进行校验
      this.$refs.form.validate(valid => {
        if (!valid) return
        // console.log('哈哈')
        axios({
          method: 'post',
          url: 'http://localhost:8888/api/private/v1/login',
          data: this.form
        }).then(res => {
          // console.log(res.data)
          // const meta = res.data.meta
          const { meta, data } = res.data
          if (meta.status === 200) {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: 'success'
            })

            // 将TOKEN字符串存在localStorage中
            localStorage.setItem('token', data.token)

            // 登录成功，跳转到首页
            // this.$router.push.push('/index')
            // this.$router.push({ path: '/index', query: { username: 'pp', password: 123456 } })
            this.$router.push({ path: 'index' })
          } else {
            this.$message({
              showClose: true,
              message: meta.msg,
              type: 'error'
            })
          }
        })
      })
    }
  }
}
</script>

// 样式
<style lang = 'less'>
.login {
  width: 100%;
  height: 100%;
  background-color: #2d434c;
  overflow: hidden;

  .el-form {
    width: 400px;
    background-color: #fff;
    margin: 200px auto;
    padding: 80px 20px 20px;
    border-radius: 20px;
    position: relative;
    .loginBtn {
      margin-right: 70px;
    }
    .tou {
      width: 120px;
      height: 120px;
      position: absolute;
      left: 50%;
      transform: translate(-50%);
      top: -70px;
      border-radius: 50%;
      border: 5px solid #fff;
    }
  }
}
</style>
